package com.shuyun.xxx.guava.practice;

import org.junit.Test;

import java.util.concurrent.*;

/**
 * Created by goodb on 2017/5/17.
 */
public class CompletionServiceTest {
    private ExecutorService executor = Executors.newFixedThreadPool(100);
    private static final int groupNum = 100000000/100;
    @Test
    public void run(){
        CompletionService<Long> completionService = new ExecutorCompletionService(executor);
        for(int i = 1; i <= 100; i++){
            int start = (i - 1) * groupNum + 1, end = i * groupNum;
            completionService.submit(() -> {
                Long sum = 0L;
                for(int j = start; j <= end; j++){
                    sum += j;
                }
                return sum;
            });
        }

        Long sum = 0L;
        try{
            for(int i = 0; i < 100; i++){
                sum += completionService.take().get();
            }
            System.out.println(sum);
        }catch(Exception e){
            e.printStackTrace();
        }
    }
}
